全部学科
Python全栈
python
NodeJS全栈
nodejs
📅 2026-05-20 8 分钟 ✍️ juanwangdev

Nginx 与操作系统内核参数配合

Nginx 性能受操作系统内核参数制约,合理调优可充分发挥 Nginx 高并发能力。

连接队列

somaxconn

Bash
# 查看当前值
cat /proc/sys/net/core/somaxconn

# 临时修改
sysctl -w net.core.somaxconn=65535

# 永久修改(/etc/sysctl.conf)
net.core.somaxconn = 65535

Nginx 配置配合

nginx
events {
    worker_connections 10240;
    # 监听队列不能超过 somaxconn
    # backlog = min(somaxconn, listen backlog)
}

server {
    listen 80 backlog=65535;
}

somaxconn 限制 listen() 的 backlog 上限,Nginx backlog 参数不能超过此值。

连接队列溢出

Bash
# 查看队列溢出
netstat -s | grep "listen"

# 输出示例:
# 1234 times the listen queue of a socket overflowed

TIME_WAIT 处理

tcp_tw_reuse

Bash
# 允许 TIME_WAIT socket 重新用于新连接
sysctl -w net.ipv4.tcp_tw_reuse=1

# 启用 tcp_tw_recycle(不推荐,NAT 环境有问题)
sysctl -w net.ipv4.tcp_tw_recycle=0

Nginx keepalive 配合

nginx
upstream backend {
    server 10.0.0.1:8080;

    # 保持与上游的长连接
    keepalive 32;
    keepalive_timeout 60s;
    keepalive_requests 1000;
}

TIME_WAIT 状态

状态说明
TIME_WAIT主动关闭方等待 2MSL
tcp_tw_reuse=1允许新连接复用 TIME_WAIT socket
tcp_fin_timeoutFIN_WAIT 超时时间,默认 60s

文件描述符

worker_rlimit_nofile

nginx
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 10240;
}

系统限制

Bash
# 查看限制
ulimit -n

# 系统级限制
cat /proc/sys/fs/file-max

# 永久修改(/etc/security/limits.conf)
nginx soft nofile 65535
nginx hard nofile 65535

文件描述符限制应 > worker_connections × worker_processes + 其他 FD(日志、缓存等)。

TCP 参数优化

TCP 缓冲区

Bash
# TCP 读写缓冲区
sysctl -w net.ipv4.tcp_rmem="4096 87380 67108864"
sysctl -w net.ipv4.tcp_wmem="4096 65536 67108864"

# 启用 TCP 窗口缩放
sysctl -w net.ipv4.tcp_window_scaling=1

连接参数

Bash
# SYN 队列大小
sysctl -w net.ipv4.tcp_max_syn_backlog=65535

# 半连接队列
sysctl -w net.ipv4.tcp_syncookies=1

# 最大连接数
sysctl -w net.ipv4.tcp_max_tw_buckets=262144

# 本地端口范围
sysctl -w net.ipv4.ip_local_port_range="1024 65535"

Nginx TCP 配置

nginx
http {
    # 启用 TCP_NODELAY
    tcp_nodelay on;

    # 启用 TCP_NOPUSH(sendfile 时优化)
    tcp_nopush on;

    # sendfile 零拷贝
    sendfile on;
}

完整调优配置

sysctl.conf

Bash
# 网络队列
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535

# TCP 参数
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_max_tw_buckets = 262144
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 30

# 内存
net.ipv4.tcp_rmem = 4096 87380 67108864
net.ipv4.tcp_wmem = 4096 65536 67108864

# 端口
net.ipv4.ip_local_port_range = 1024 65535

# 文件
fs.file-max = 1000000

nginx.conf

nginx
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 10240;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    keepalive_requests 100;
}

要点总结

  • somaxconn 限制 listen backlog 上限,Nginx backlog 参数不能超过此值
  • tcp_tw_reuse=1 允许 TIME_WAIT socket 复用,减少连接建立开销
  • worker_rlimit_nofile 应 > worker_connections × worker_processes,避免文件描述符耗尽
  • tcp_nopush + sendfile 配合优化大文件发送,tcp_nodelay 降低小数据延迟
  • 内核参数通过 sysctl.conf 持久化,Nginx 配置应与内核参数匹配
想在手机上练习这篇文章的配套题目?
使用微信卷王开发者小程序,打开首页顶部扫码功能识别二维码
← 上一篇 Nginx 内存池与共享内存优化
下一篇 → 健康检查机制
扫码体验小程序
加载中
想在手机上刷题学习?
使用微信卷王开发者小程序,打开首页顶部扫码功能识别二维码